<!--  其他设施 -->
<template>
  <div style="padding: 15px 15px">
    <div style="text-align: right">
      <el-button type="primary" @click="addFacility" size="mini" v-if="!btnStatus">添加</el-button>
      <el-button type="primary" @click="approveSubmit" size="mini" v-if="!btnStatus && model.projectStatus == 1">批量提交</el-button>
      <el-button type="primary" @click="approveFacility" size="mini" v-if="!btnStatus && model.projectStatus == 2">批量提审</el-button>
      <el-button type="primary" size="mini" @click="draftFn" v-if="isPageEdit">{{ btnStatus ? '未生效版本' : '已生效版本' }}</el-button>
    </div>
    <div style="height: 20px"></div>
    <div v-if="favilityList.length > 0">
      <el-form label-width="120px" size="small" label-position="right" v-for="(item, index) in favilityList" :key="index">
        <el-row>
          <el-col :span="8">
            <el-form-item label="设施名称">
              <el-input v-model="item.stationName" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所在楼层">
              <el-input v-model="item.floorName" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="设施状态">
              <el-input v-model="item.statusName" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24" style="text-align: right; margin-top: -15px">
            <el-button type="text" size="small" @click="infoFn(item, true)">详情</el-button>
            <el-button type="text" size="small" class="color-red" @click="deleteFn(item)" v-if="!btnStatus">删除</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-empty v-else description="暂无设施"></el-empty>

    <facilityDialog ref="facilityDialog" @uploadData="getInfo('draft')" :work-id.sync="workId"></facilityDialog>
  </div>
</template>
<script>
import facilityDialog from '../modules/dialog/facilityDialog';
import projectInfoApi from '../../../api/projectInfo';
import { requestTips, askDialog } from '../../../assets/util';
export default {
  components: { facilityDialog },
  data() {
    return {
      favilityList: [],
      btnStatus: true,
      workId: '',
      mddel: {},
      projectKey:this.$route.query.projectKey || localStorage.getItem("projectKey")
    };
  },
  computed: {
    isPageEdit() {
      return !this.$route.query.taskId;
    },
  },
  mounted() {
    this.getInfo();
    this.getProjectInfo();
  },
  //方法集合
  methods: {
    //查询最新审核版本
    async getInfo(type) {
      let apiName = type == 'draft' ? 'apiFacilityDraft' : 'apiFacilityList';
      let res = await projectInfoApi[apiName]({
        projectKey: this.projectKey,
        status: 1,
        workId: type == 'draft' ? this.workId : null,
      });
      res.rows.forEach(item => {
        item.statusName = item.status == 1 ? '正常' : '删除';
      });
      this.favilityList = res.rows || [];
    },
    async getProjectInfo() {
      let res = await projectInfoApi.apiProjectDetail({
        primaryKey:this.projectKey
      });
      this.model = res;
    },
    //添加设施
    addFacility() {
      this.$refs['facilityDialog'].addFacility();
    },
    approveSubmit() {
      askDialog('确认提交？', async () => {
        let data = {
          workType: 4,
          projectKey: this.projectKey,
          workId: this.workId,
        };
        const res = await projectInfoApi.summitAuditAndSuccess(data);
        requestTips(res, () => {
          this.getInfo();
          this.getProjectInfo();
          this.btnStatus = true;
        });
      });
    },
    //提审
    async approveFacility() {
      let pathUrl = {
        name: 'arraignment-facility',
        query: {
          ...this.$route.query,
          workId: this.workId,
        },
      };
      let { href } = this.$router.resolve(pathUrl);
      window.open(href);
      // askDialog('确认提审？', async () => {
      //   let data = {
      //     workType: 4,
      //     projectKey: this.projectKey,
      //     workId: this.workId,
      //   };
      //   let res = await projectInfoApi.summitAudit(data);
      //   requestTips(res, () => {
      //     this.getInfo();
      //     this.btnStatus = true;
      //   });
      // });
    },
    //删除
    deleteFn(data) {
      askDialog('确认删除？', async () => {
        const res = await projectInfoApi.apiFacilityDelete({ primaryKey: data.primaryKey });
        requestTips(res, () => {
          this.getInfo('draft');
        });
      });
    },
    //详情编辑
    infoFn(data) {
      data.fileUrl = '';
      this.$refs['facilityDialog'].infoFacility(data,this.btnStatus);
    },
    //未生效版本
    async draftFn() {
      if (this.btnStatus) {
        let res = await projectInfoApi.getWorkId({ projectKey: this.projectKey, workType: 4 });
        //1-待审核//2-审核中//3-通过//4-不通过
        if (res.procStatus == 2) {
          this.$message.warning('审批中，无草稿数据');
          return;
        }
        this.workId = res.workId;
        this.btnStatus = !this.btnStatus;
        this.getInfo('draft');
      } else {
        this.getInfo();
        this.btnStatus = !this.btnStatus;
      }
    },
  },
};
</script>
<style scoped>
/* @import url(); 引入css类 */
</style>
